<template>
  <div>
    <div>
      <el-input
          size="small"
          class="addPosInput"
          placeholder="添加职位"
          prefix-icon="el-icon-plus"
          v-model="pos.name">
      </el-input>
      <el-button icon="el-icon-plus" size="small" type="primary" @click="addPosition" >添加</el-button>
    </div>
    <div class="posManaMain">
      <el-table
          :data="positions"
          border
          stripe
          size="small"
          @selection-change="handleSelectionChange"
          style="width: 70% ">
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            prop="id"
            label="编号"
            width="55">
        </el-table-column>
        <el-table-column
            prop="name"
            label="职位名称"
            width="180">
        </el-table-column>
        <el-table-column
            prop="createdate"
            width="150"
            label="创建时间">
        </el-table-column>
        <el-table-column label="是否启用">
          <template slot-scope="scope">
            <el-tag size="small" type="success" v-if="scope.row.enabled">已启用</el-tag>
            <el-tag size="small" type="danger" v-else>未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="showEditView(scope.$index,scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="handleDelete(scope.$index,scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button size="small" type="danger" style="margin-top: 8px"
      :disabled="multipleSelection.length==0" @click="deleteMany">批量删除</el-button>
    </div>

    <el-dialog
        title="修改职位"
        :visible.sync="dialogVisible"
        width="30%">
      <div>
        <div>
          <el-tag>职位名称</el-tag>
          <el-input style="width: 300px ;margin-left: 8px" size="small" v-model="updatePos.name"></el-input>
        </div>
        <div>
          <el-tag>是否启用</el-tag>
          <el-switch
              v-model="updatePos.enabled"
              active-text="启用"
              inactive-text="禁用">
          </el-switch>
        </div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="doUpdate">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {postRequest} from "../../../utils/api";
import {getRequest} from "../../../utils/api";
import {deleteRequest} from "../../../utils/api";
import {putRequest} from "../../../utils/api";

export default {
  name: "PosMana",
  data(){
    return{
      pos:{
        name:''
      },
      positions:[],
      multipleSelection: [],
      dialogVisible: false,
      updatePos:{
        id:'',
        name:'',
        enabled: false,
      }
    }
  },
  mounted() {
    this.initPositions();
  },
  methods:{
    deleteMany(){
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = '?';
        this.multipleSelection.forEach((item)=>{
          ids += 'ids='+item.id+'&';
        })
        deleteRequest("/system/basic/pos/"+ids).then((res)=>{
          if(res){
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.initPositions();
          }
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    doUpdate(){
      putRequest("/system/basic/pos/",this.updatePos).then((res)=>{
        if(res){
          this.initPositions();
          this.updatePos={};
          //this.$message.success("更新成功！");
          this.dialogVisible = false;
        }
      })
    },
    showEditView(index,data){
      this.updatePos.name = data.name;
      this.updatePos.id = data.id;
      this.updatePos.enabled = data.enabled;
      this.dialogVisible = true;
    },
    handleSelectionChange(val){
      this.multipleSelection = val;
    },
    initPositions(){
      getRequest("/system/basic/pos/").then((res)=>{
        if(res){
          this.positions = res;
        }
      })
    },
    addPosition(){
      if(this.pos.name){
        postRequest("/system/basic/pos/",this.pos).then((res)=>{
          if(res){
            this.initPositions();
            this.pos.name = ''
          }
        })
      }else{
        this.$message.error("职位名称不可以为空！");
      }
    },
    handleDelete(index,data){
      this.$confirm('此操作将永久删除【'+data.name+'】职位，是否继续？','提示',{
        confirmButtonText:'确定',
        cancelButtonText:'取消',
        type:'warning'
      }).then(()=>{
        deleteRequest("/system/basic/pos/"+data.id).then((res)=>{
          if (res){
            this.initPositions();
          }
        })
      }).catch(()=>{
        this.$message({
          type:'info',
          message:'已取消删除'
        })
      })
    },
  }
}
</script>

<style scoped>
.posManaMain{
  margin-top: 8px;
}
.addPosInput{
  width: 300px;
  margin-right: 8px
}
</style>